$(document).ready(function() {
	$('.tipsy').tipsy({gravity: 's'});// nw | n | ne | w | e | sw | s | se --- http://onehackoranother.com/projects/jquery/tipsy/
	$('#pexpired').datepicker({showButtonPanel: true});		
	$('#pexpired').datepicker('option', {dateFormat: 'dd/mm/yy'},$.datepicker.regional['vi']);
	$("#frmEdit").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {
			ptitle: {
				required				: true,
				minlength				: 5
			},
			pcategoryid:{
				min						: 1
			},
			pquantity: {
				max						: 100
			},
			pexpired: {
				validExpired			: true
			},
			phours: {				
				validHours				: true
			},
			pprovince: {
				min						: 1
			},
			pdistrict: {
				min						: 1
			},
			paddress: {
				required				: true
			},
			pmobile: {
				required				: true
			},
			pcontent: {
				required				: true,
				minlength				: 20
			}
		},
		messages: {	
			ptitle: {
				required				: name_required,
				minlength				: name_minlength
			},
			pcategoryid:{
				min						: cate_required
			},
			pquantity: {
				max						: quantity_max
			},
			pexpired: {
				validExpired			: expired_invalid
			},
			phours: {				
				validHours				: hours_invalid
			},
			pprovince: {
				min						: province_required
			},
			pdistrict: {
				min						: district_required
			},
			paddress: {
				required				: address_required
			},
			pmobile: {
				required				: mobile_required
			},
			pcontent: {
				required				: content_required,
				minlength				: content_minlength
			}
		},
		errorPlacement: function(label, element) {
			var id = element.attr('id');	
			if(id == "pcategoryid") {
				label.insertAfter($('.category'));			
			} else if(id == "phours") {
				label.insertAfter($('#pminutes'));			
			} else if(id == "pprovince") {
				label.insertAfter($('.province'));			
			} else if(id == "pdistrict") {
				label.insertAfter($('.district'));			
			} else {
				label.insertAfter($('#' + id));			
			}
		}				 
	});
	$.validator.addMethod('validExpired',function(){
		var strExpired 	= $('#pexpired').val();
		var intHours 	= $('#phours').val();
		var intMinute	= $('#pminutes').val();	
		if(strExpired != "")
			return checkDate(strExpired, 0);
		else if(strExpired == "" && (intHours != "" || intMinute != ""))
			return false;
		return true;
	});
	$.validator.addMethod('validHours',function(){
		var intHours 	= $('#phours').val();
		var intMinute	= $('#pminutes').val();		
		if(intHours == "" && intMinute != "")
			return false;
		else if(intHours != "" && intMinute == "")
			return false;
		else if((intHours != "" && intHours.length != 2) || (intMinute != "" && intMinute.length != 2))
			return false;
		else if(intHours != "" && intMinute != "" && ( parseInt(intHours) > 23 || parseInt(intMinute) > 60) ) 
			return false;		
		return true;
	});
	
	$("textarea#pcontent").autoResize({
		// On resize:
		onResize : function() {
			$(this).css({opacity:0.8});
		},
		// After resize:
		animateCallback : function() {
			$(this).css({opacity:1});
		},
		// Quite slow animation:
		animateDuration : 300,
		// More extra space:
		extraSpace : 5
	});
	
});
district = function(intProvince, divDistrict) {			
	$.getJSON(
		HOST_VFRIEND + "ajax/district?province=" + intProvince + "&callback=?",		
		function(response) {				
			if(response["data"] != "") {								
				$("#"+divDistrict).html(response["data"]);						
			}
			else {
				$("#"+secDistrict).hide();
			}
		}
	);							   
};	
function lookup(inputString) {	
	inputString = html_entity_decode(inputString);	
	if(inputString.length < 3 ) {		
		$('#suggestions').hide();
	} else {	
		$('#suggestions').show();
		$('#autoSuggestionsList').html('<p>&nbsp;</p><p align="center"><img src="'+ HOST_STATICS+'img/bg/loading-bar.gif" alt="diem tin,ket ban,tim ban" width="88" height="7" /><br/>Bạn vui lòng chờ trong giây lát.<br/></p><p>&nbsp;</p>');
		var intProvince = $('#pprovince').val();
		var intDistrict	= $('#pdistrict').val();				
		$('#addr').html('');
		$('#fulladdr').hide();
		$('#psrcid').val(0);
		$.getJSON(
			HOST_VFRIEND + 'ajax/smartsearchoff?val=' + encodeURIComponent(inputString) + '&province=' + intProvince + '&district=' + intDistrict + '&callback=?',		
			function(response) {				
				if(response['data'] != '') {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(response['data']);
				}
				else {										
					setTimeout("$('#suggestions').hide();", 300);
				}
			}
		);
	}
} // lookup

function selectItem(thisValue,thisHidden,thisAddr,thisDistrict) {
	$('#paddress').val(thisValue);
	$('#psrcid').val(thisHidden);
	$('#fulladdr').show();
	$('#addr').html(thisAddr);
	$('#suggestions').hide();
}

function hideLookup() {
	var intSrcId = $('#psrcid').val();
	var strAddr	 = $('#paddress').val();
	if(intSrcId > 0 && strAddr != "") {
		$('#suggestions').hide();
	} else {
		setTimeout("$('#suggestions').hide();", 500);
	}
}